﻿using System.ComponentModel;
using System.Windows;
using Moonwish.Core;
using Moonwish.Foundation.ValueObjects;

namespace Moonwish.Foundation.ViewModels.ValueObjects
{
    /// <summary>
    /// 主窗体ViewModel，write by zhlin 2010-03-15
    /// </summary>
    public class ValueObjectMainViewModel : ViewModelMain
    {
        private ValueObjectModel _model;
        private static ValueObjectClassViewModel _classViewModel;
        private static ValueObjectViewModel _valueObjectViewModel;

        protected override void Initialization()
        {
            if (!IsInDesignMode)
            {
                PrepareViewModel();

                QueryBusinessData();
            }
        }

        private void PrepareViewModel()
        {
            _model = new ValueObjectModel();

            ValueObjectClassViewModel.PropertyChanged += OnViewModelPropertyChanged;
            ValueObjectViewModel.PropertyChanged += OnViewModelPropertyChanged;
            ValueObjectClassViewModel.SelectedItemChanged += (s, arg) =>
            {
                if (_classViewModel.SelectedItem != null)
                {
                    ValueObjectViewModel.ClassId = _classViewModel.SelectedItem.Id;
                    ValueObjectViewModel.Data = _classViewModel.SelectedItem.ValueObjects;
                }
                else
                {
                    ValueObjectViewModel.ClassId = string.Empty;
                }
            };
        }

        private void QueryBusinessData()
        {
            var request = new Request {What = ValueObjectModel.QueryIdentityClass};
            IsBusy = true;
            _model.QueryCompleted += (s, e) =>
            {
                IsBusy = false;
                if (e.Error != null)
                {
                    MessageBox.Show(e.Error.Message);
                    return;
                }
                _classViewModel.Data = _model.ValueObjectClasses;
                if (_model.ValueObjectClasses.Count > 0)
                {
                    _classViewModel.SelectedItem = _model.ValueObjectClasses[0];

                    _valueObjectViewModel.Data = _classViewModel.SelectedItem.ValueObjects;

                    if (_valueObjectViewModel.Data.Count > 0)
                    {
                        _valueObjectViewModel.SelectedItem = _valueObjectViewModel.Data[0];
                    }
                }
            };
            _model.QueryAsync(request);
        }

        private void OnViewModelPropertyChanged(object sender, PropertyChangedEventArgs args)
        {
            if (args.PropertyName == "IsBusy")
            {
                IsBusy = ((ViewModelBase)sender).IsBusy;
            }
        }

        public static ValueObjectClassViewModel ValueObjectClassViewModel
        {
            get { return _classViewModel ?? (_classViewModel = new ValueObjectClassViewModel()); }
        }

        public static ValueObjectViewModel ValueObjectViewModel
        {
            get { return _valueObjectViewModel ?? (_valueObjectViewModel = new ValueObjectViewModel()); }
        }
    }
}
